package com.jin.ks;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;
import android.widget.Toast;

/**
 * Created by lenovo on 2018/10/16.
 */

public class UtilDao {
    private DatabaseUtil du;
    private SQLiteDatabase db;

    public UtilDao(Context context){
        du = new DatabaseUtil(context);
        db = du.getWritableDatabase();
    }


    /**
     * 添加数据
     * */
    public void addData(String tableName,String[] key,String[] values){
        ContentValues contentValues = new ContentValues();
        for(int i = 0; i < key.length; i ++){
            contentValues.put(key[i],values[i]);
        }
        db.insert(tableName,null,contentValues);
        contentValues.clear();
    }

    /**
    *选中状态
     */
    public void status(String value){
        //update table set date = 'ppp' where date = 'ssss'
        db.execSQL("update UserInfo set mStatus = '0'");
        db.execSQL("update UserInfo set mStatus = '1' where mName='"+value+"'");
    }

    /**
     * 删除数据
     * */
    public int delData(String where,String[] values){
        int del_data;
        del_data = db.delete("UserInfo",where,values);
        return del_data;
    }

    /**
     * 修改数据
     * */
    public void update(String[] values){
        db.execSQL("update UserInfo set mStatus=?,mName=?,mKuaiId=?,mFan=?,mFollow=?,mMoment=?,mLike=?,mPhoto=?,mText=?,mBirthday=?,mTodayFollow=?,mTime=? where mName=? ",values);
    }

    /**
     * 查询数据
     * */
    public List<User> inquireData(){
        List<User> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select mStatus,mName,mKuaiId,mFan,mFollow,mMoment,mLike,mPhoto,mText,mBirthday,mTodayFollow,mTime" +
                                    " from UserInfo",null);
        
        while(cursor.moveToNext()){
            String status = cursor.getString(0);
            String name = cursor.getString(1);
            String kuaiId = cursor.getString(2);
            String fan = cursor.getString(3);
            String follow = cursor.getString(4);
            String moment = cursor.getString(5);
            String like = cursor.getString(6);
            String photo = cursor.getString(7);
            String text = cursor.getString(8);
            String birthday = cursor.getString(9);
            String todayFollow = cursor.getString(10);

            User user = new User();
            user.setStatus(status);
            user.setName(name);
            user.setKwaiId(kuaiId);
            user.setFan(fan);
            user.setFollow(follow);
            user.setMoment(moment);
            user.setLike(like);
            user.setPhoto(photo);
            user.setText(text);
            user.setBirthday(birthday);
            user.setTodayFollow(todayFollow);
            

            list.add(user);
        }

        return list;
    }
    
    /**
    *查询指定数据
    **/
    
    public User inquireEditData(String value){
        User user = new User();
        Cursor cursor = db.rawQuery("select * from UserInfo where mName=?",new String[]{value});
        if (cursor.moveToNext()) {
            // 取出数据集中的单行数据的个字段的数据
            // cursor.getColumnIndex("id");获取指定的字段的下标
            // cursor.getInt(index);或getString(index);是获取指定的下标的数据index为int类型;
            user.setStatus(cursor.getString(1));
            user.setName(cursor.getString(2));
            user.setKwaiId(cursor.getString(3));
            user.setFan(cursor.getString(4));
            user.setFollow(cursor.getString(5));
            user.setMoment(cursor.getString(6));
            user.setLike(cursor.getString(7));
            user.setPhoto(cursor.getString(8));
            user.setText(cursor.getString(9));
            user.setBirthday(cursor.getString(10));
            user.setTodayFollow(cursor.getString(11));
            user.setTime(cursor.getString(12));
            
        }
        // 关闭数据库连接,释放资源
		
        
        return user;
    }

    /**
     * 关闭数据库连接
     * */
    public void getClose(){
        if(db != null){
            db.close();
        }
    }
}
